package com.dongdong.zhuangji.utils;

/**
 * @author dongdong 抖音号：W13883155245
 * @version 1.0
 * @date 2025/5/3
 * @description 数值转换成里程数
 */
public class NumberToKilometerFormat {
    public static void main(String[] args) {
        double num1 = 100328.512;
        double num2 = 28.312;
        String num3 = 1211+"";
        System.out.println(formatNumberToKilometer(num1,1));
        System.out.println(formatNumberToKilometer(num2,2));
        System.out.println(formatNumberToKilometer(num3,3));
    }

    /**
     * 100328.512=> K100+328.5
     * @param number 要转换的里程数
     * @param n 小数位数
     * @return
     */
    public static String formatNumberToKilometer(double number,int n) {
        int kilometers = (int) (number / 1000);
        double remainder = number % 1000;
        return String.format("K%d+%."+n+"f", kilometers, remainder);
    }
    public static String formatNumberToKilometer(String k,int n) {
        double number = Double.parseDouble(k);
       return formatNumberToKilometer(number,n);
    }
}